package com.system.cacheCenterBackend.controller.hotKeyController;

import com.system.cacheCenterBackend.service.HotKeyService.HotKeyCounterService;
import com.system.cacheClient.common.CacheR;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.CompletableFuture;

/**
 * 缓存控制器，提供RESTful API接口
 */
@RestController
@RequestMapping("/cache/hotKey")
@RequiredArgsConstructor
public class HotKeyController {


    private final HotKeyCounterService hotKeyCounterService;

    /**
     * 获取key的访问次数
     *
     * @param key 键
     * @return 统一返回结果
     */
    @GetMapping("/access-count/{key}")
    public CacheR<Integer> getKeyAccessCount(@PathVariable String key) {
        try {
            Integer count = hotKeyCounterService.getKeyCount(key);
            return CacheR.success(count, "获取访问次数成功");
        } catch (Exception e) {
            return CacheR.fail("获取访问次数失败: " + e.getMessage());
        }
    }

    /**
     * 异步获取key的访问次数
     *
     * @param key 键
     * @return 统一返回结果
     */
    @GetMapping("/access-count-async/{key}")
    public DeferredResult<CacheR<Integer>> getKeyAccessCountAsync(@PathVariable String key) {
        DeferredResult<CacheR<Integer>> deferredResult = new DeferredResult<>();
        
        CompletableFuture<Integer> future = hotKeyCounterService
                .getKeyCountAsync(key);
        
        future.whenComplete((result, throwable) -> {
            if (throwable != null) {
                deferredResult.setResult(CacheR.fail("异步获取访问次数失败: " + throwable.getMessage()));
            } else {
                deferredResult.setResult(CacheR.success(result, "异步获取访问次数成功"));
            }
        });
        
        return deferredResult;
    }

    /**
     * 检查key是否为热点key
     *
     * @param key 键
     * @return 统一返回结果
     */
    @GetMapping("/hot/{key}")
    public CacheR<Boolean> isHotKey(@PathVariable String key) {
        try {
            boolean isHot = hotKeyCounterService.isHotKey(key);
            return CacheR.success(isHot, "检查热点key成功");
        } catch (Exception e) {
            return CacheR.fail("检查热点key失败: " + e.getMessage());
        }
    }

    /**
     * 重置指定键的访问统计
     *
     * @param key 键
     * @return 统一返回结果
     */
    @PostMapping("/reset/{key}")
    public CacheR<Boolean> resetKeyStatistics(@PathVariable String key) {
        try {
            hotKeyCounterService.resetKeyStatistics(key);
            return CacheR.success(true, "重置访问统计成功");
        } catch (Exception e) {
            return CacheR.fail("重置访问统计异常: " + e.getMessage());
        }
    }

    /**
     * 添加键到热点数据统计
     *
     * @param type 缓存类型
     * @param key  键
     */
    public static void addKey(String type, String key) {
        // 这里可以添加热点键的统计逻辑
        // 目前为空实现，后续可以扩展
    }

    /**
     * 从热点数据统计中移除键
     *
     * @param type 缓存类型
     * @param key  键
     */
    public static void removeKey(String type, String key) {
        // 这里可以添加移除热点键的逻辑
        // 目前为空实现，后续可以扩展
    }
}